package com.minyifang.wcspf.work.ui.home.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.minyifang.wcspf.R;
import com.minyifang.wcspf.work.ui.home.model.StoreListBean;

import java.util.List;

public class ChooseStoreAdapter extends RecyclerView.Adapter<ChooseStoreAdapter.Holder> {

    private List<StoreListBean.StoreChildListBean> mStoreList;
    private Context mContext;
    private int mDefaultColor = Color.parseColor("#666666");
    private int mChoseColor;
    private int mCurChosePosition = -1;

    private OnItemClickListener mOnItemClickListener;

    public ChooseStoreAdapter(Context context, List<StoreListBean.StoreChildListBean> stringList) {
        mStoreList = stringList;
        mContext = context;
        mChoseColor = ContextCompat.getColor(context, R.color.colorPrimary);
    }

    public void setCurChosePosition(int curChosePosition) {
        mCurChosePosition = curChosePosition;
        notifyDataSetChanged();
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(curChosePosition);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(mContext).inflate(R.layout.item_store_choose, parent, false);
        return new Holder(layout);
    }

    @Override
    public void onBindViewHolder(@NonNull final Holder holder, int position) {
        if (position > -1) {
            if (position == mCurChosePosition) {
                holder.mTvTitle.setTextColor(mChoseColor);
                holder.mTvDesc.setTextColor(mChoseColor);
                holder.mTvChooice.setVisibility(View.VISIBLE);
            } else {
                holder.mTvTitle.setTextColor(mDefaultColor);
                holder.mTvDesc.setTextColor(mDefaultColor);
                holder.mTvChooice.setVisibility(View.GONE);
            }
        }
        holder.mTvTitle.setText(mStoreList.get(position).getName());
        holder.mTvDesc.setText("距离你" + mStoreList.get(position).getDistance() + "米");

        holder.mTvChooice.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemChoiceClick(holder.getAdapterPosition());
            }
        });


        holder.itemView.setOnClickListener(v -> {
            mCurChosePosition = holder.getAdapterPosition();
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(holder.getAdapterPosition());
            }
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return mStoreList.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView mTvTitle;
        TextView mTvDesc;
        TextView mTvChooice;

        Holder(View itemView) {
            super(itemView);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            mTvDesc = itemView.findViewById(R.id.tv_desc);
            mTvChooice = itemView.findViewById(R.id.tv_choice);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onItemChoiceClick(int position);
    }
}